﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyTestProject
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var cardPrefix = ConfigurationManager.AppSettings["cardPrefix"];
            LBCardNo.Text = "";
            for (int i = 0; i < 100; i++)
            {
                var code = "";
                for (int j = 0; j < 4; j++)
                {
                    code += Guid.NewGuid().ToString().Substring(4, 4).ToUpper();
                    if (j < 3) code += "-";
                }
                var no = GetRandomCardNo(cardPrefix);
                LBCardNo.Text += no + "&nbsp;" + no.Length + "&nbsp;" + code + "<br />";
            }
        }

        private string GetRandomChar()
        {
            var chars = new[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
            var ran = new Random();
            var n = ran.Next(chars.Length - 1);
            return chars[n];
        }

        private string GetRandomCardNo(string cardPrefix)
        {
            var str = "0x" + Guid.NewGuid().ToString().Substring(4, 4).ToUpper() +
                      Guid.NewGuid().ToString().Substring(4, 4).ToUpper();
            var suffix = Math.Abs(Convert.ToInt32(str, 16));
            var no = cardPrefix + DateTime.Now.ToString("yyMMdd") + GetRandomChar() + suffix;
            return no;
        }
    }
}